﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace LibArchivumAeternum
{
    public class ConfigFile
    {
        public ConfigFile()
        {
        }

        public static Configuration Load(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
            return (Configuration) serializer.Deserialize(File.OpenRead(fileName));
        }

        public static void Save(string fileName, Configuration configuration)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
            serializer.Serialize(File.OpenWrite(fileName), configuration);
        }

        public static void GenerateExample(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
            serializer.Serialize(File.OpenWrite(fileName), new Configuration()
                {
                    Version = "1",
                    Tasks = new List<Task>()
                    {
                        new Task()
                        {
                            Name = "Regular backup",

                            Sources = new List<string>()
                            {
                                "c:\\Users\\guest\\Desktop",
                                "d:\\Data"
                            },

                            Destination = "E:\\Backup",

                            Excludes = new List<Exclude>()
                            {
                                new Exclude() { Pattern = ".*\\.bak", ApplyTo = ApplyTo.Files },
                                new Exclude() { Pattern = ".*\\~.*", ApplyTo = ApplyTo.Files | ApplyTo.Directories }
                            },

                            EqualityCheck = EqualityCheck.ModificationDate | EqualityCheck.Size | EqualityCheck.CheckSum
                        },
                        new Task()
                        {
                            Name = "Mini backup",

                            Sources = new List<string>()
                            {
                                "d:\\Data"
                            },

                            Destination = "E:\\Mini Backup",

                            Excludes = new List<Exclude>()
                            {
                                new Exclude() { Pattern = ".*\\.bak", ApplyTo = ApplyTo.Files },
                                new Exclude() { Pattern = ".*\\~.*", ApplyTo = ApplyTo.Files }
                            },

                            EqualityCheck = EqualityCheck.CheckSum
                        }
                    }
                });
        }
    }
}